package com.guoguo.sincemusic.logic.netwrok

import com.guoguo.sincemusic.BuildConfig
import com.guoguo.sincemusic.logic.dao.Storage
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 * 创建 Retrofit构造器
 * 日志参考 : https://blog.csdn.net/yu540135101/article/details/113034404
 * @author GuoGuo
 * @date 2021/12/15
 */

object ServiceCreator {
    private var okHttpClient: OkHttpClient

    init {
        val client = OkHttpClient.Builder()
        //header中追加参数
        client.addInterceptor(AppendHeaderParamInterceptor())
        //url追加参数
        client.addInterceptor(AppendUrlParamInterceptor())
        //在Debug模式下设置日志拦截器
        if (BuildConfig.DEBUG) {
            val logger =
                HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }
            client.addInterceptor(logger)
        }
        okHttpClient = client.build()
    }

    private const val BASE_URL = "https://music.gspguoguo.top"

    // Retrofit构造器 生成Retrofit实例
    private val retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    // 对外提供的方法
    fun <T> create(serviceClass: Class<T>): T = retrofit.create(serviceClass)
    inline fun <reified T> create(): T = create(T::class.java)
}

/**
 * url追加参数
 */
class AppendUrlParamInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        //1.获取到请求
        val request = chain.request()
        //2.得到之前的url
        val builder = request.url.newBuilder()
        //3.追加信息，得到新的url
        var newUrl = builder
        //如果登录了添加新的url
        if (Storage.isLogin()) {
            val userInfo = Storage.getUserInfo()
            val url = builder
                .addQueryParameter("cookie", userInfo.cookie)
                .addQueryParameter("token", userInfo.token)
            newUrl = url
        }

        //4.新的url创建新的request
        val newRequest = request.newBuilder()
            .url(newUrl.build())
            .build()
        //5.返回
        return chain.proceed(newRequest)
    }

}

/**
 * header追加参数
 */
class AppendHeaderParamInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        //1.得到request
        val request = chain.request()
        //2.得到之前的header
        val builder = request.headers.newBuilder()
        //3.追加新的header信息
        var newHeader = builder
        //如果登录了添加新的header
        if (Storage.isLogin()) {
            val userInfo = Storage.getUserInfo()
            val header = builder
                .add("cookie", userInfo.cookie)
                .add("token", userInfo.token)
            newHeader = header
        }

        //4.创建一个新的请求
        val newRequest = request.newBuilder()
            .headers(newHeader.build())
            .build()
        //5.返回
        return chain.proceed(newRequest)
    }

}